package com.games.game.mapper;

import com.games.game.bean.Game;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GamesMapper {
    static String JDBC_URL =  "jdbc:mysql://localhost/games?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT";//?allowPublicKeyRetrieval=true&useSSL=false&characterEncoding=utf8";
    static String JDBC_USER = "root";
    static String JDBC_PASSWORD = "root";

    public List<Game> get_all() {
        load();
        ArrayList<Game> list = new ArrayList<>();
        try (Connection conn = (Connection) DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD)) {
            try (PreparedStatement ps = conn.prepareStatement(
                    "SELECT * FROM games")) {
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    list.add(get_game_from_rs(rs));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public Game get_cer(String name) {
        load();
        try (Connection conn = (Connection) DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD)) {
            try (PreparedStatement ps = conn.prepareStatement(
                    "SELECT * FROM games WHERE name = ?")) {
                ps.setObject(1, name);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    return get_game_from_rs(rs);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Game> get_cer_num_games(int num) {
        load();
        ArrayList<Game> list = new ArrayList<>();
        int cnt = 0;
        try (Connection conn = (Connection) DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD)) {
            try (PreparedStatement ps = conn.prepareStatement(
                    "SELECT * FROM games ORDER BY purchase_time DESC")) {
                ResultSet rs = ps.executeQuery();
                while (rs.next() && cnt < num) {
                    cnt++;
                    list.add(get_game_from_rs(rs));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<Game> get_cer_type_games(String type) {
        load();
        ArrayList<Game> list = new ArrayList<>();
        try (Connection conn = (Connection) DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD)) {
            try (PreparedStatement ps = conn.prepareStatement(
                    "SELECT * FROM games WHERE category = ?")) {
                ps.setObject(1, type);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    list.add(get_game_from_rs(rs));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public void purchase(String name) {
        load();
        try (Connection conn = (Connection) DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD)) {
            try (PreparedStatement ps = conn.prepareStatement(
                    "UPDATE games SET purchase_time=purchase_time+1 WHERE name = ?")) {
                ps.setObject(1, name);
                ps.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void refund(String name) {
        load();
        try (Connection conn = (Connection) DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD)) {
            try (PreparedStatement ps = conn.prepareStatement(
                    "UPDATE games SET purchase_time=purchase_time-1 WHERE name = ?")) {
                ps.setObject(1, name);
                ps.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void load() {
        try {
            // The newInstance() call is a work around for some
            // broken Java implementations
            Class.forName("com.mysql.cj.jdbc.Driver");
//            System.out.println("加载成功");
        } catch (Exception ex) {
            System.out.println("加载失败");
            // handle the error
        }
    }

    private Game get_game_from_rs(ResultSet rs)  {
        String g_name = null;
        try {
            g_name = rs.getString(2);
            String cat = rs.getString(3);
            String des = rs.getString(4);
            int price = rs.getInt(5);
            String photo_1 = rs.getString(6);
            String photo_2 = rs.getString(7);
            Float score = rs.getFloat(8);
            int ti = rs.getInt(9);
            return new Game(g_name, cat, des, price, photo_1, photo_2, score, ti);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
